package com.cheng.common.utils;

import com.cheng.common.exception.CommonException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;

/**
 * @author: ChengJW
 * @date: 2021/11/1 20:03
 * @version: 1.0
 * @description: 字符串 工具类
 */
public class StringUtil {

    private final static Logger log = LoggerFactory.getLogger(StringUtil.class);

    /**
    * @author: ChengJW
    * @params: [str]
    * @return: boolean
    * @date: 2021/11/2
    * @description: 判断字符串是否为空
    */
    public static boolean isNull(String str){
        return "".equals(str) || null == str || str.length() == 0;
    }

   /**
   * @author: ChengJW
   * @params: [str]
   * @return: boolean
   * @date: 2021/11/2
   * @description: 判断字符串不为空
   */
    public static boolean isNotNull(String str){
        return !isNull(str);
    }

    /**
    * @author: ChengJW
    * @params: [list]
    * @return: boolean
    * @date: 2021/11/2
    * @description: 判断集合是否为空
    */
    public static boolean isNull(List<Object> list){
        return  null == list || list.size() == 0;
    }

    /**
    * @author: ChengJW
    * @params: [list]
    * @return: boolean
    * @date: 2021/11/2
    * @description: 判断集合不为空
    */
    public static boolean isNotNull(List<Object> list){
        return  !isNull(list);
    }

    /**
    * @author: ChengJW
    * @params: [map]
    * @return: boolean
    * @date: 2021/11/2
    * @description: 判断 map 是否为空
    */
    public static boolean isNull(Map<String,Object> map){
        return null == map || map.size() == 0;
    }


    /**
    * @author: ChengJW
    * @params: [map]
    * @return: boolean
    * @date: 2021/11/2
    * @description: 判断 map 是否为空
    */
    public static boolean isNotNull(Map<String,Object> map){
        return !isNull(map);
    }

    /**
    * @author: ChengJW
    * @params: [str, start, end]
    * @return: java.lang.String
    * @date: 2021/11/2
    * @description: 截取字符串
    */
    public static String subString(String str,int start,int end) throws CommonException{
        if (isNull(str)){
            log.info("字符串为空，无法进行操作！");
            throw new CommonException("CJW202111010001","StringUtil","subString");
        }
        if (start < 0 || end < 0 || start > end){
            log.info("参数不正确，无法进行操作！");
            throw new CommonException("CJW202111010002","StringUtil","subString");
        }
        return str.substring(start,end);
    }

    /**
    * @author: ChengJW
    * @params: [str]
    * @return: java.lang.String
    * @date: 2021/11/2
    * @description: 字符串转大写
    */
    public static String upString(String str) throws CommonException{
        if (isNull(str)){
            log.info("字符串为空，无法进行操作！");
            throw new CommonException("CJW202111010001","StringUtil","upString");
        }
        return str.toUpperCase();
    }

    /**
    * @author: ChengJW
    * @params: [str]
    * @return: java.lang.String
    * @date: 2021/11/2
    * @description: 字符串转小写
    */
    public static String lowString(String str) throws CommonException{
        if (isNull(str)){
            log.info("字符串为空，无法进行操作！");
            throw new CommonException("CJW202111010001","StringUtil","lowString");
        }
        return str.toLowerCase();
    }

    /**
    * @author: ChengJW
    * @params: [str, start]
    * @return: boolean
    * @date: 2021/11/2
    * @description: 判断字符串是否以指定字符串开始
    */
    public static boolean startWith(String str,String start) throws CommonException{
        if (isNull(str)){
            log.info("字符串为空，无法进行操作！");
            throw new CommonException("CJW202111010001","StringUtil","startWith");
        }
        return str.startsWith(start);
    }

    /**
    * @author: ChengJW
    * @params: [str, start]
    * @return: boolean
    * @date: 2021/11/2
    * @description: 判断字符串是否以指定字符串结束
    */
    public static boolean endsWith(String str,String start) throws CommonException{
        if (isNull(str)){
            log.info("字符串为空，无法进行操作！");
            throw new CommonException("CJW202111010001","StringUtil","endsWith");
        }
        return str.endsWith(start);
    }

    public static String [] split(String str,String s) throws CommonException{
        if (isNull(str)){
            log.info("字符串为空，无法进行操作！");
            throw new CommonException("CJW202111010001","StringUtil","split");
        }
        return str.split(s);
    }
}
